package com.generator.sysManage.userManage.service.impl;

import com.generator.sysManage.userManage.dao.IUMMap;
import com.generator.sysManage.userManage.pojo.Dept;
import com.generator.sysManage.userManage.pojo.Job;
import com.generator.sysManage.userManage.pojo.Users;
import com.generator.sysManage.userManage.pojo.Users1;
import com.generator.sysManage.userManage.service.IUMService;
import com.generator.util.PageModel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * UMService
 * @author WJW
 * @Date .....
 * @version ......
 */
@Service
public class UMService implements IUMService {

    @Autowired(required = true)
    private IUMMap iumMap;

    public void setIumDao(IUMMap iumMap) {
        this.iumMap = iumMap;
    }

    public IUMMap getIumDao() {
        return iumMap;
    }

    /**
     * 根据条件查询所有用户集合
     * @param user 查询条件
     * @return 用户集合
     */
    public List<Users> getUserListByUser(Users user){
        return iumMap.getUserListByUser(user);
    }


    /**
     * 根据条件查询所有用户集合进行分页
     * @param users 查询条件
     * @return 用户集合
     */
    public PageModel<Users> getUserListByFY(Users users,PageModel<Users> pageModel){
        pageModel.setPageNum(pageModel.getPageNum()<=0?1:pageModel.getPageNum());
        pageModel.setPageSize(pageModel.getPageSize()<=0?3:pageModel.getPageSize());
        PageHelper.startPage(pageModel.getPageNum(),pageModel.getPageSize());
        List<Users> userList = iumMap.getUserListByUser(users);


        PageInfo<Users> pageInfo = new PageInfo<Users>(userList);

        pageModel.setData(new ArrayList<Users>(userList));
        pageModel.setTotalPage(pageInfo.getPages());
        pageModel.setTotalRows(pageInfo.getTotal());

        return pageModel;
    }


    //查询所有部门
    public List<Dept> getDeptList(Dept dept){
        return iumMap.getDeptList(dept);
    }

    //查询所有部门
    public List<Job> getJobList(Job job){
        return iumMap.getJobList(job);
    }

    //查询单个用户信息
    public List<Users> queryOne(Users1 user){
        return iumMap.queryOne(user);
    }


    //添加用户
    public Integer addUser(Users1 user){
        return iumMap.addUser(user);
    }

    //修改用户
    public Integer updateUser(Users1 user){
        return iumMap.updateUser(user);
    }

    //修改用户职位状态
    public String  updateStatu(Users users, Model model){
        String u_id1 = users.getU_id();
        String[] split = u_id1.split(",");
        String u_id=split[0];
        Users user=new Users();
        user.setU_id(u_id);
        user.setLeavedate(new Date());
        int pageNum=Integer.parseInt(split[1]);
        PageModel<Users> pageModel=new PageModel<>();
        pageModel.setPageNum(pageNum);
        System.out.println(user);
        Integer add = iumMap.updateStatu(user);
        if(add==1){
            Users user1=new Users();
            PageModel<Users> userList = getUserListByFY(user1,pageModel);
            model.addAttribute("userList",userList);
            return "sys/users/userList";
        }
        return "forward:/userManage/userList";
    }

}
